/*
 summary:
		通用类库
 auhtor:
		李华顺<huacnlee@gmail.com>
 date:
		2008-09-29
*/
var Common = function(){};


/**
 * coder-blog Javascript 全局设置文件
 */
Common.Settings = {
	/**
	 * 是否在调试状态
	 */
	DEGUG : true,

	/**
	 * Ajax的调用方式，以便于终于管理，用GET的时候好调试
	 */
	AJAX_TYPE : "POST",
	
	/**
	 * Scripts 的根目录
	 */
	SCRIPTS_PATH : "/static/scripts/"
}

/**
 * @summary 执行状态，用于函数的返回值
 * @author 李华顺<huacnlee@gmail.com>
 */
Common.ExecuteState = function(){

	/**
	 * 执行是否成功
	 */
	this.success = true;
	/**
	 * 提示消息
	 */
	this.message = "";
	/**
	 * 错误标识,如：username或password
	 */
	this.mark = "";
	/**
	 * 当前被验证的对象
	 */
	this.owner = undefined;

	/**
	 * 自定义dict
	 */
	this.dict = {}
}

/**
 * 助手类
 */
Common.Helper = {
	/**
	 * 引入JS
	 * 用法：
	 * 		引用内部网站的JS文件就不用带scripts根目录的地址了，默认会加上 "/static/scripts/"
	 * 		如果要引用外部文件，请传入第二个参数为true
	 * @param {String} filename 文件名
	 * @param {Boolean} outside 是否是引用外部文件
	 * @author 李华顺 <huacnlee@gmail.com>
	 * @date 2008-10-07
	 */
	includeJS : function(filename,outside){
		var src = filename;
		if(outside == undefined && outside != true){
			src =  Common.Settings.SCRIPTS_PATH + filename;
		}

		var script = '<script type="text/javascript" src="'+src+'"></script>';
		document.body.appendChild($(script)[0]);
	}
}


/**
 * @summary Coderblogs可视化文本编辑器
 * @author 李华顺<huacnlee@gmail.com>
 */
Common.Editor = {

	varsion : "0.1",

	/**
	 * @summary 创建可视化编辑器
	 * @params
	 *		elementId <object> 创建到个对象里面
	 *		id <string> 编辑器的 elementId
	 * 		textId <string> 文本框的elementId
	 * 		textName <string> 文本框的name
	 */
	create : function(elementId,rows){

		if(! tinyMCE){
			alert("还未引用TinyMCE的JS文件。");
			return false;
		}

		var owner = $("#"+elementId);
		var editorid = "editor-box-"+elementId;
		var elementName = owner.attr("name");
		var content = owner.val();

		if(rows == undefined){
			rows = 24;
		}

		var result = EDITOR_TEMPLATE;
		result = result.replace('{{editorid}}',editorid);
		result = result.replace('{{textId}}',elementId);
		result = result.replace('{{textId}}',elementName);
		result = result.replace('{{value}}',content);
		result = result.replace('{{rows}}',rows);

		owner.after(result);
		owner.remove();

		var editor = $('#' + editorid);
		var txtBody = $('#' + elementId);
		var subButtons = $(".sub-buttons",editor)
		var btnShow = $('.buttons .view button.show',editor);

		// 绑定事件
		btnShow.click(function(){
			$('.view button',editor).removeClass('selected');
			$(this).addClass('selected');

			txtBody.hide();
			subButtons.hide();
			txtBody.focus();
			 $('#' + elementId + '_parent').show();
		});

		$('.buttons .view button.code',editor).click(function(){
			$('.view button',editor).removeClass('selected');
			$(this).addClass('selected');

			txtBody.show();
			txtBody.focus();
			subButtons.show();
			 $('#' + elementId + '_parent').hide();
		});

		// tinyMCE init
		tinyMCE.init({
			mode : "textareas",
			editor_selector : "tinymce",
			theme : "advanced",
			language : "zh",
			convert_urls : false,
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_buttons1 : "fullscreen,separator,formatselect,fontselect,separator,bold,italic,underline,strikethrough,separator,bullist,numlist,outdent,indent,separator,undo,redo,separator,link,unlink,anchor,separator,cleanup,separator",
			theme_advanced_buttons2 : "",
			theme_advanced_buttons3 : "",
			auto_cleanup_word : true,
			plugins : "table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,zoom,flash,searchreplace,contextmenu,fullscreen",
			plugin_insertdate_dateFormat : "%m/%d/%Y",
			plugin_insertdate_timeFormat : "%H:%M:%S",
			extended_valid_elements : "a[name|href|target=_blank|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
			fullscreen_settings : {
				theme_advanced_path_location : "top",
				theme_advanced_buttons1 : "fullscreen,separator,preview,separator,cut,copy,paste,separator,undo,redo,separator,search,replace,separator,code,separator,cleanup,separator,bold,italic,underline,strikethrough,separator,forecolor,backcolor,separator,justifyleft,justifycenter,justifyright,justifyfull,separator,help",
				theme_advanced_buttons2 : "removeformat,styleselect,formatselect,fontselect,fontsizeselect,separator,bullist,numlist,outdent,indent,separator,link,unlink,anchor",
				theme_advanced_buttons3 : "sub,sup,separator,image,insertdate,inserttime,separator,tablecontrols,separator,hr,advhr,visualaid,separator,charmap,emotions,iespell,flash,separator,print"
			},
			theme_advanced_resizing : true,
			theme_advanced_resize_horizontal : false	
		});
		return true;
	}

}


// Coderblogs Editor HTML 模板
EDITOR_TEMPLATE = '\
<div id="{{editorid}}" class="tinymce-box">\
	<div class="buttons">\
		<div class="insert">\
			上传/插入 <span class="link"><a href="#" class="insert-image"></a><a href="#" class="insert-file"></a></link>\
		</div>\
		<div class="view">\
			<button	type="button" class="show selected">可视化</button><button type="button" class="code">代码</button>\
		</div>\
		<div class="clear"></div>\
	</div>\
	<div id="edit-box">	\
		<div class="sub-buttons" style="display:none;"></div>		\
		<textarea id="{{textId}}" class="tinymce text long" rows="{{rows}}">{{value}}</textarea>\
	</div>\
</div>';




/*
 summary:
	字符串类方法扩展
 auhtors:
	 李华顺 <huacnlee@gmail.com>
 date:
	2008-09-29
*/

/**
 * 是否是整形字符串
 * @author 李华顺 <huacnlee@gmail.com>
 */
String.prototype.isInt32 = function(){
	var regex = new RegExp("^[\d]{1,32}$");
	return regex.test(this);
}
/**
 * 验证是否是合法的日期格式,只接受 2008-10-1 或 2008/01/11 类类似以 - 或 / 分开的日期
 */
String.prototype.isDate = function(){
	var regex = new RegExp("^[\d]{4}([-|/])[\d]{1,2}([-|/])[\d]{1,2}$");
	return regex.test(this);
}
/**
 * 验证是否是合法的Username格式
 */
String.prototype.isUserName = function(){
	var regex = new RegExp("^[a-zA-Z0-9\-_]{4,16}$");
	return regex.test(this);
}
/**
 * 验证是否是合法的Email格式
 */
String.prototype.isEmail = function(){
	var regex = new RegExp("^([a-zA-Z0-9_-])+(\.([a-zA-Z0-9_-])+)*@([a-zA-Z0-9_-])+(\.([a-zA-Z0-9_-])+)+$");
	return regex.test(this);
}

/**
 * 是否是一个合法的自定义域名
 */
String.prototype.isSlug = function(){
	var regex = new RegExp("[a-zA-Z0-9_-]{4,100}");
	return regex.test(this);
}

/**
 * 是否是Url地址
 */
String.prototype.isUrl = function(){
	var regex = /http:\/\/([\w-]+\.)+[\w-]+(\/[\w-   .\/?%&=]*)?/;
	return regex.test(this);
}

/**
 * 去掉最后个字符
 * @param {Object} c 想去的字符
 */
String.prototype.removeLastChar = function(c){
	if (this.substr(this.length - 1, 1) == c) {
		return this.substr(0, this.length - 1);
	}
	else {
		return this;
	}
}

/**
 * 去两边的空格
 */
String.prototype.trim = function(){
	return this.replace(/^\s+|\s+$/g, "");
}
/**
 * 去右边空格
 */
String.prototype.trimRight = function(){
	return this.replace(/\s+$/, "");
}
/**
 * 去左边空格
 */
String.prototype.trimLeft = function(){
	return this.replace(/^\s+/, "");
}
